package com.sky.zzh.setting;

import org.json.JSONObject;

import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;

import com.next.intf.ITaskListener;
import com.next.net.SHPut;
import com.next.net.SHTask;
import com.sky.widget.SHDialog;
import com.sky.widget.SHToast;
import com.sky.widget.sweetdialog.SweetDialog;
import com.sky.zzh.BaseFragment;
import com.sky.zzh.R;
import com.sky.zzh.util.CommonUtil;
import com.sky.zzh.util.ConfigDefinition;
import com.sky.zzh.util.UserInfoManager;

/**
 * 修改密码
 * 
 * @author skypan
 * 
 */
public class ChangePwdFragment extends BaseFragment implements ITaskListener{

	private EditText mEtOld, mEtNew, mEtNew2;
	private Button mBtnSave;
	private ImageView mBtnBack;
	private SHPut updateTask;

	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		View view = inflater.inflate(R.layout.fragment_change_pwd, container, false);
		return view;
	}

	@Override
	public void onViewCreated(View view, Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onViewCreated(view, savedInstanceState);
		mEtOld = (EditText) view.findViewById(R.id.et_old);
		mEtNew = (EditText) view.findViewById(R.id.et_new);
		mEtNew2 = (EditText) view.findViewById(R.id.et_new2);
		mBtnSave = (Button) view.findViewById(R.id.btn_save);
		mBtnBack = (ImageView) view.findViewById(R.id.btn_back);
		setListeners();
	}

	private void setListeners() {
		mBtnBack.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View arg0) {
				// TODO Auto-generated method stub
				getActivity().finish();
			}
		});
		
		mBtnSave.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View arg0) {
				// TODO Auto-generated method stub
				String oldPwd = mEtOld.getText().toString().trim();
				String newPwd = mEtNew.getText().toString().trim();
				String newPwd2 = mEtNew2.getText().toString().trim();
				if (CommonUtil.isEmpty(oldPwd)) {
					SHToast.showToast(getActivity(), "请输入旧密码");
					return;
				}

				if (CommonUtil.isEmpty(newPwd)) {
					SHToast.showToast(getActivity(), "请输入新密码");
					return;
				}

				if (CommonUtil.isEmpty(newPwd2)) {
					SHToast.showToast(getActivity(), "请确认新密码");
					return;
				}
				
				if(!newPwd.equals(newPwd2)){
					SHToast.showToast(getActivity(), "新密码不一致");
					return;
				}

				SHDialog.ShowProgressDiaolg(getActivity());
				updateTask = new SHPut();
				updateTask.setUrl(ConfigDefinition.URL + "user/ModifyUserInfo");
				updateTask.setListener(ChangePwdFragment.this);
				updateTask.getTaskArgs().put("UserID", UserInfoManager.getInstance().getUserId());
				updateTask.getTaskArgs().put("Password", newPwd);
				updateTask.start();
			}
		});
	}

	@Override
	public void onTaskFinished(SHTask task) throws Exception {
		// TODO Auto-generated method stub
		JSONObject json = new JSONObject(task.getResult().toString());
		if ("200".equals(json.optString("ReturnCode"))) {
			SHToast.showToast(getActivity(), "修改成功");
			getActivity().finish();
		}else{
			new SweetDialog(getActivity(), SweetDialog.ERROR_TYPE).setTitleText("提示").setContentText(json.optString("ErrorMessage")).show();
		}
	}

	@Override
	public void onTaskFailed(SHTask task) {
		// TODO Auto-generated method stub
		
	}

	@Override
	public void onTaskUpdateProgress(SHTask task, int count, int total) {
		// TODO Auto-generated method stub
		
	}

	@Override
	public void onTaskTry(SHTask task) {
		// TODO Auto-generated method stub
		
	}
}
